home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15006 < prev    next >
Encoding:
Text File  |  1996-08-05  |  4.0 KB  |  148 lines

  1. Path: news.nask.org.pl!usenet
  2. From: flssoft@blue.maloka.waw.pl (Grzegorz (FLS))
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Unions
  5. Date: Tue, 02 Apr 1996 23:44:07 GMT
  6. Organization: Research and Academic Computer Network
  7. Message-ID: <4jsads$1a3@bilbo.nask.org.pl>
  8. References: <rossfeld.1.00111D6B@ucla.edu>
  9. NNTP-Posting-Host: s110.maloka.waw.pl
  10. X-Newsreader: Forte Free Agent v0.46
  11.  
  12. rossfeld@ucla.edu (James E Rossfeld) wrote:
  13.  
  14. >Could someone give me a detailed explanation of what the advantages are to 
  15. >using unions (if any).  I'm still a little unfamiliar with how they work.
  16.  
  17. Hi,
  18.  
  19. Imagine that you write a calc parser that could have saved variables.
  20. You can use it in such way:
  21.  
  22. First=5
  23. TheSecond=10
  24. ?=First
  25. First=First+TheSecond*10
  26.  
  27. etc
  28.  
  29. When you analyse each line, for example 'First=First+TheSecond*10',
  30. you collect (build) special tree like: (sorry, there is only a text)
  31.  
  32.                       =
  33.                     /   \
  34.               First      +
  35.                        /   \
  36.                  First      *
  37.                           /   \
  38.                 TheSecond      10
  39.  
  40. This is a prefix tree. You analyse the left side first, next you
  41. analyse the operator, and next analyse the right side. For example,
  42. first you get 'First' variable, next you get '=' sign, and you know,
  43. that you have to save in 'First' variable the result of right subtree.
  44. But lets return to our union.
  45. Each node of that tree could save:
  46. operators like '=', '-', '+'
  47. variables like 'First', 'TheSecond'
  48. values like 10, 3.1415926
  49.  
  50. Lets assume that we do not use unions. In this way we write a
  51. structure for each node like:
  52.  
  53. struct SNode
  54. {
  55.     // What we are saving here in node.
  56.     enum
  57.     {
  58.         SN_OPERATOR
  59.         , SN_VARIABLE
  60.         , SN_VALUE
  61.  
  62.     } m_what_is_here ;
  63.  
  64.     // The content of node.
  65.     char   m_operator ; // like '=', '-', '*', '/' etc
  66.     char*  m_p_variable_name ;
  67.     double m_value ;
  68.  
  69.     // Left and right subtrees.
  70.     SNode* m_pLeftNode ;
  71.     SNode* m_pRightNode ;
  72. } ;
  73.  
  74.  
  75. Lets count the sizeof( SNode ) for FAR memory model (on PC)
  76.  
  77.     m_what_is_here    = sizeof( int )    =  2
  78.     m_operator        = sizeof( char )   =  2
  79.     m_p_variable_name = sizeof( char* )  =  4
  80.     m_value           = sizeof( double ) =  8
  81.     m_pLeftNode       = sizeof( SNode* ) =  4
  82.     m_pLeftRight      = sizeof( SNode* ) =  4
  83.   -------------------------------------------
  84.                          sizeof( SNode ) = 24
  85.  
  86.  
  87. Note that in _every_ node you reserve memory for each situation
  88. (variable, operator, value), however we are sure, that there is only
  89. one possible state of each node. There is no possibility to have in
  90. the same node both variable and value (for example). Then for what we
  91. declare this memory? It is only a waste of it.
  92. Lets optimize it. Now we will use an union.
  93.  
  94. struct SNode
  95. {
  96.     // What we are saving here in node.
  97.     enum
  98.     {
  99.         SN_OPERATOR
  100.         , SN_VARIABLE
  101.         , SN_VALUE
  102.  
  103.     } m_what_is_here ;
  104.  
  105.     // The content of node.
  106.     union
  107.     {
  108.         char   m_operator ; // like '=', '-', '*', '/' etc
  109.         char*  m_p_variable_name ;
  110.         double m_value ;
  111.     }
  112.  
  113.     // Left and right subtrees.
  114.     SNode* m_pLeftNode ;
  115.     SNode* m_pRightNode ;
  116. } ;
  117.  
  118.  
  119. What happened? What gives us this union?
  120. Lets count the sum of sizes for each members of union.
  121.  
  122.     m_operator        = sizeof( char )   =  2
  123.     m_p_variable_name = sizeof( char* )  =  4
  124.     m_value           = sizeof( double ) =  8
  125.    -------------------------------------------
  126.                                          = 14
  127.  
  128. and what is the size of the union? Lets look:
  129.  
  130.               The size is max( 2, 4, 8 ) = 8
  131.  
  132. The compile declare the space for the biggest member of the union. Now
  133. each node will have more less size - 18 (not 24). You save 25% of
  134. memory. In this way you can build the bigger tree, You can add 25%
  135. more nodes.
  136.  
  137. A union saves you memory, gives you a different look at declaration.
  138. There is no need to use it often. Only sometimes (like in the example)
  139. there is need to do it.
  140.  
  141. I hope it will explain you a union.
  142. Maybe my english won't disturb you? ;)
  143.  
  144. Good luck,
  145. Grzegorz.
  146.  
  147.  
  148.